package com.cnc.bas.common;
/*
 * Copyright 2007 by ChinanetCenter Corporation.
 *
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * ChinanetCenter Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with ChinanetCenter.
 * 
 */
import java.util.List;

import javax.servlet.http.HttpServletRequest;
 

import com.cnc.bas.ConfigManager;

/**
 * 查询分页管理抽象管理类，子类有HqlWrapper(Hibernate查询方式)及SqlWrapper(iBatis查询方式)。<br>
 * <p/>
 * 在进行分页查询时，可使用此类为参数，并返回相应查询结果及总计记录数(如果需要)
 *
 * @author hongdj
 * @version 1.5
 */
public abstract class AbstractQueryInfo {

    //查询结果的起始位置
    private int offset;

    // 返回页大小
    private int pageSize;

    //当前页码
    private int pageNo;

    //总页数
    private int pageCount;

    //总记录数
    private int recordCount;

    //是否要统计总记录数,默认要
    private boolean countTotalSize ;

    //查询结果列表
    private List queryList;

	private boolean showAll;

    /**
     * 根据HttpServletRequest创建hql对象
     *
     * @param request 得到当前页
     */
    public AbstractQueryInfo(HttpServletRequest request) {
        this.pageSize = 0;
        this.pageNo = 0;
        this.pageCount = 0;
        this.recordCount = 0;
        this.offset = -1;
        this.queryList = null;
        this.countTotalSize = true;
        this.showAll = false;
        //以下根据前台传的参数赋值
        if (request != null) {
        	int intCurrentPage = 1;
        	String currPage = request.getParameter("intCurrentPage");
        	try{
        		intCurrentPage = Integer.parseInt(currPage);
        	}
        	catch(Exception e){
        	}
        	
        	int intTotalPages = 1;
        	String totalPage = request.getParameter("intTotalPages");
        	try{
        		intTotalPages = Integer.parseInt(totalPage);
        	}
        	catch(Exception e){
        		
        	}
        	 //如果intTotalPages=-1,全部取出
        	 //取配置文件powerweb.properties,默认为15条
        	if(intTotalPages==-1)
        		showAll = true;
        	this.pageSize = Integer.parseInt(ConfigManager.getInstance().getConfigValue("PageSize", "20"));;
            this.pageNo = intCurrentPage;
            this.offset = pageSize * (pageNo - 1);
        }
    }

    public AbstractQueryInfo() {
        this.pageSize = 0;
        this.pageNo = 0;
        this.pageCount = 0;
        this.recordCount = 0;
        this.offset = -1;
        this.queryList = null;
        this.countTotalSize = true;
    }
    /**
     * 获取页大小
     *
     * @return int
     */
    public int getPageSize() {
        return pageSize < 0 ? 0 : pageSize;
    }

    /**
     * 设置页大小
     *
     * @param pageSize int
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
        this.offset = this.pageSize * (this.pageNo - 1);
    }

    /**
     * 获取当前页号
     *
     * @return int
     */
    public int getPageNo() {
        return pageNo < 0 ? 0 : pageNo;
    }

    /**
     * 设置当前页号
     *
     * @param pageNo 页号
     */
    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
        this.offset = this.pageSize * (this.pageNo - 1);
    }

    /**
     * 查询结果的起始位置
     *
     * @return int
     */
    public int getOffset() {
        return offset < 0 ? 0 : offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

    /**
     * 总页数
     *
     * @return int
     */
    public int getPageCount() {
        if (pageSize <= 0) {
            this.pageCount = 0;
        } else {
            this.pageCount = (recordCount - 1) / pageSize + 1;
        }
        return pageCount;
    }

    /**
     * 总记录数
     *
     * @return int
     */
    public int getRecordCount() {
        return recordCount < 0 ? 0 : recordCount;
    }

    public void setRecordCount(int recordCount) {
        this.recordCount = recordCount;
    }

    /**
     * 查询结果列表
     *
     * @return List
     */
    public List getQueryList() {
        return queryList;
    }

    public void setQueryList(List queryList) {
        this.queryList = queryList;
    }

    /**
     * 是否要统计总记录数
     *
     * @return boolean
     */
    public boolean isCountTotalSize() {
        return countTotalSize;
    }

    public void setCountTotalSize(boolean countTotalSize) {
        this.countTotalSize = countTotalSize;
    }

	public boolean isShowAll() {
		return showAll;
	}

	public void setShowAll(boolean showAll) {
		this.showAll = showAll;
	}
}
